package cn.raven.order.service.impl;

import cn.raven.basic.exception.BusinessException;
import cn.raven.basic.service.impl.BaseServiceImpl;
import cn.raven.basic.utils.CodeGenerateUtils;
import cn.raven.basic.utils.LoginContext;
import cn.raven.order.domain.OrderProduct;
import cn.raven.order.domain.OrderProductDetail;
import cn.raven.order.domain.OrderProductLog;
import cn.raven.order.dto.OrderConfirmationDto;
import cn.raven.order.dto.OrderDto;
import cn.raven.order.mapper.OrderProductDetailMapper;
import cn.raven.order.mapper.OrderProductLogMapper;
import cn.raven.order.mapper.OrderProductMapper;
import cn.raven.order.service.IOrderProductService;
import cn.raven.org.domain.Employee;
import cn.raven.serve.domain.Product;
import cn.raven.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Raven
 * @since 2022-07-21
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private OrderProductDetailMapper orderProductDetailMapper;
    @Autowired
    private OrderProductLogMapper orderProductLogMapper;

    /**
    * @Title: buy
    * @Description: 生成服务订单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 12:28
    * @Parameters: [searchMasterMsg]
    * @Return void
      */
    @Override
    public void buy(Product product, HttpServletRequest request) {

        //非空校验
        if (StringUtils.isEmpty(product.getName()) ||
                StringUtils.isEmpty(product.getResources()) ||
                StringUtils.isEmpty(product.getSaleprice())){
            throw new BusinessException("参数不能为空");
        }
        User user = LoginContext.getUser(request);
        OrderProduct orderProduct = new OrderProduct();
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        orderProduct.setOrderSn(orderSn);
        orderProduct.setDigest("购买服务订单："+product.getName());
        orderProduct.setState(0);
        BigDecimal bigDecimal = new BigDecimal("1.5");
        orderProduct.setPrice(product.getSaleprice().multiply(bigDecimal));
        orderProduct.setPaySn("0");
        orderProduct.setLastPayTime(new Date(System.currentTimeMillis() + 2*24*60*60*1000));
        orderProduct.setProductId(product.getId());
        orderProduct.setUserId(user.getId());
        orderProductMapper.add(orderProduct);
        OrderProductDetail detail = new OrderProductDetail();
        detail.setName(product.getName());
        detail.setResources(product.getResources());
        detail.setSaleprice(product.getSaleprice().multiply(bigDecimal));
        detail.setOffsaletime(product.getOffsaletime());
        detail.setOnsaletime(product.getOnsaletime());
        detail.setState(product.getState());
        detail.setCostprice(product.getCostprice().toString());
        detail.setProductId(product.getId());
        detail.setOrderId(orderProduct.getId());
        orderProductDetailMapper.add(detail);
    }
    
    /** 
    * @Title: accept
    * @Description: 待处理服务接单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 14:45  
    * @Parameters: [dto, request]
    * @Return void
      */
    @Override
    public void accept(OrderDto dto, HttpServletRequest request) {

        Employee employee = LoginContext.getEmployee(request);
        OrderProduct orderProduct = orderProductMapper.findOne(dto.getOrderId());
        //给当前订单shopId赋值，表示已被店铺接单
        orderProduct.setShopId(employee.getShopId());
        orderProductMapper.update(orderProduct);
        //加上日志
        OrderProductLog log = new OrderProductLog();
        log.setOrderProductid(dto.getOrderId());
        log.setState(0);
        log.setAuditTime(new Date());
        log.setNote("店铺id为"+orderProduct.getShopId()+"接单");
        orderProductLogMapper.add(log);
    }

    /**
    * @Title: confirm
    * @Description: 确认订单
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/7/21 15:42
    * @Parameters: [dto]
    * @Return void
      */
    @Override
    public void confirm(OrderConfirmationDto dto) {
        OrderProduct orderProduct = orderProductMapper.findOne(dto.getId());
        if (orderProduct.getState() == -1){
            throw new BusinessException("已取消的不可再确认");
        }
        orderProduct.setState(1);
        orderProduct.setPrice(dto.getMoney());
        orderProduct.setPaySn(dto.getPayType().toString());
        orderProductMapper.update(orderProduct);
        //订单详情
        OrderProductDetail detail = orderProductDetailMapper.findByOrderProductId(orderProduct.getProductId());
        detail.setState(1L);
        detail.setSaleprice(dto.getMoney());
        orderProductDetailMapper.update(detail);
        //设置日志
        OrderProductLog log = new OrderProductLog();
        log.setOrderProductid(orderProduct.getId());
        log.setState(1);
        log.setAuditTime(new Date());
        log.setNote("订单完成");
        orderProductLogMapper.add(log);
    }

    @Override
    public void cancel(Long id) {

        OrderProduct orderProduct = orderProductMapper.findOne(id);
        if (orderProduct.getState() == 1 ){
            throw new BusinessException("完成的不可取消哦");
        }
        orderProduct.setState(-1);
        orderProduct.setShopId(null);
        orderProductMapper.update(orderProduct);
        //订单详情
        OrderProductDetail detail = orderProductDetailMapper.findByOrderProductId(orderProduct.getProductId());
        detail.setState(-1L);
        orderProductDetailMapper.update(detail);
        //记录失败日志
        OrderProductLog log = new OrderProductLog();
        log.setOrderProductid(orderProduct.getId());
        log.setState(-1);
        log.setAuditTime(new Date());
        log.setNote("订单取消");
        orderProductLogMapper.add(log);
    }
}
